//
//  HJUserManager.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJUserManager.h"
#import "HJNavigationViewController.h"
#import "HJLoginVC.h"

#define UserInfoFilePath ([[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"user_info.file"]])
#define TaskGamerInfoFilePath ([[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"TaskGamer_info.file"]])

@implementation HJUserManager

/// 同步个人信息
+ (void)syncUserModel {
    [self syncUserModelWithCompletion:nil];
}

+ (void)syncUserModelWithCompletion:(void (^)(HJUserModel *model))completion {
    NSString * url = [Api stringByAppendingString:QueryByIdApi];
    [HJHttpTool get:url params:@{@"userId":[self getUserModel].userId} success:^(HJResponeds *result) {
        [self setUserModelWithDict:result.data];
        HJUserModel *model = [HJUserModel yy_modelWithJSON:result.data];
        completion(model);
    } failure:^(id result) {
    }];
}
/// 获得当前登录用户的个人信息
+ (HJUserModel *)getUserModel {
    NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithFile:UserInfoFilePath];
    HJUserModel *model = [HJUserModel yy_modelWithJSON:dict];
    return model;
}

/// 设置当前登录用户的个人信息
+ (void)setUserModelWithDict:(NSDictionary *)dict {
    BOOL saveSuccess = [NSKeyedArchiver archiveRootObject:dict toFile:UserInfoFilePath];
    HJUserModel *model = [HJUserModel yy_modelWithJSON:dict];
    [mUserDefaults setObject:model.token forKey:@"token"];
    [mUserDefaults synchronize];
    if (!saveSuccess) {
        NSLog(@"个人信息保存失败");
    }
}
/// 是否是登录状态,如果不是，弹出登录控制器
+ (BOOL)isLogin {
    BOOL isLogon = [[mUserDefaults objectForKey:@"isLogin"] isEqualToString:@"1"];
    if (!isLogon) {
        HJLoginVC *loginVC = [[HJLoginVC alloc]init];
        [[UIViewController currentViewController] presentViewController:loginVC animated:YES completion:nil];
        return NO;
    }
    return YES;
}



+(void)updateUserModelWithParams:(NSDictionary *)params success:(void(^)(void))success {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    if (params) {
        [dic addEntriesFromDictionary:params];
    }
    [dic setValue:[self getUserModel].userId forKey:@"userId"];
    
    NSString * url = [Api stringByAppendingString:userEditApi];
    @weakify(self)
    [YFHud showProgress:@"更新中"];
    [HJHttpTool post:url params:dic success:^(HJResponeds *result) {
        @strongify(self)
        LKLog(@"%@", result);
        [self setUserModelWithDict:result.data];
        [YFHud showSuccessInWindow:@"更新成功"];
        [mNotificationCenter postNotificationName:@"updateUINotification" object:nil];
        if (success) {
            success();
        }
    } failure:^(id result) {
        [YFHud showError:result];
    }];
}
@end

/*********************************************** 用户信息模型 ****************************************************************/


@implementation HJUserModel
//+(NSDictionary *)mj_objectClassInArray {
//    return @{ @"brand_list":[brandListModel class],@"default_size":[defaultSizeModel class]};
//}

+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
    
    return @{
             @"cityModel":@"city",
             @"tagModels":@"tags"
            };
}

+ (nullable NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass {
    return @{
             @"tagModels": [YFUserInfoTagModel class],
             };
}

- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
    
    [self transformProperties];
    
    return YES;
}

#pragma mark - 加工数据
- (void)transformProperties {
    
    // 性别 Image
    NSString *sexImageName = self.sex.integerValue == 1 ? @"ico_mine_male" : @"ico_mine_female";
    self.sexImage = [UIImage imageNamed:sexImageName];
    
    // 用户标签数组 与 标签颜色数组
    NSMutableArray *tagArray = [NSMutableArray arrayWithCapacity:self.tagModels.count];
    NSMutableArray *tagColorArray = [NSMutableArray arrayWithCapacity:self.tagModels.count];
    
    for (YFUserInfoTagModel *tagModel in self.tagModels) {
        [tagArray addObject:tagModel.name];
        [tagColorArray addObject:[UIColor withHexString:@"#000000"]];
    }
    
    self.tagArray = [tagArray mutableCopy];
    self.tagColorArray = [tagColorArray mutableCopy];
}

- (NSString *)description {
    return [self yy_modelDescription];
}
@end



@implementation CityModel

- (NSString *)description {
    return [self yy_modelDescription];
}


@end

@implementation  YFUserInfoTagModel

- (NSString *)description {
    return [self yy_modelDescription];
}

@end


@implementation YFUserAuthModel

- (NSString *)description {
    return [self yy_modelDescription];
}

@end

/*********************************************** 用户信息模型 ****************************************************************/
